//
//  YMCellForHotDetail.m
//  VideoPic
//
//  Created by apple on 16/3/30.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "YMCellForHotDetail.h"
#import "YMModelForReply.h"
#import <YYWebImage.h>


@interface YMCellForHotDetail () {
    CGFloat replyHeight;
}

@property (nonatomic, strong) UIImageView *imageViewOfUserFace;
@property (nonatomic, strong) UILabel *labelOfUserName;
@property (nonatomic, strong) UILabel *labelOfReplyTime;
@property (nonatomic, strong) UILabel *labelOfReplyContent;

@end

@implementation YMCellForHotDetail

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews {
    self.imageViewOfUserFace = [[UIImageView alloc] init];
    [self.contentView addSubview:_imageViewOfUserFace];
    
    
    
    self.labelOfUserName = [[UILabel alloc] init];
    [self.contentView addSubview:_labelOfUserName];
    self.labelOfUserName.textColor = [UIColor grayColor];
    self.labelOfUserName.font = [UIFont systemFontOfSize:15];

    
    self.labelOfReplyTime = [[UILabel alloc] init];
    [self.contentView addSubview:_labelOfReplyTime];
    self.labelOfReplyTime.textColor = [UIColor grayColor];
    self.labelOfReplyTime.font = [UIFont systemFontOfSize:15];
    self.labelOfReplyTime.textAlignment = NSTextAlignmentRight;
    
    self.labelOfReplyContent = [[UILabel alloc] init];
    [self.contentView addSubview:_labelOfReplyContent];
    self.labelOfReplyContent.font = [UIFont systemFontOfSize:15];
    self.labelOfReplyContent.numberOfLines = 0;
}

- (void)handleDataWithModel:(YMModelForReply *)model {
    [self.imageViewOfUserFace yy_setImageWithURL:[NSURL URLWithString:model.icon] options:YYWebImageOptionProgressiveBlur |YYWebImageOptionSetImageWithFadeAnimation];
    self.labelOfUserName.text = model.nick;
    
    NSDate *tempDate = [NSDate dateWithTimeIntervalSince1970:model.createTime.doubleValue];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *strDate = [dateFormatter stringFromDate:tempDate];
    [dateFormatter setDateFormat:@"MM-dd"];
    NSString *str2Date = [dateFormatter stringFromDate:tempDate];
    
    NSDate *now = [NSDate date];
    NSTimeInterval time = [now timeIntervalSince1970];
    float seconds = (float)time - model.createTime.doubleValue;
    float points = seconds / 60;
    float hours = points / 60;
    float day = hours / 24;
    if (points <= 1) {
        self.labelOfReplyTime.text = [NSString stringWithFormat:@"刚刚"];
    }else if (points < 60) {
        self.labelOfReplyTime.text = [NSString stringWithFormat:@"%d分钟前", (int)points];
        
    }else if (hours >= 1 && hours <= 24) {
        self.labelOfReplyTime.text = [NSString stringWithFormat:@"%@",strDate];
    }else if(day >= 1 && day < 2) {
        self.labelOfReplyTime.text = [NSString stringWithFormat:@"昨天"];
    }else if(day >= 2 && day < 3) {
        self.labelOfReplyTime.text = [NSString stringWithFormat:@"前天"];
    }else{
        self.labelOfReplyTime.text = [NSString stringWithFormat:@"%@",str2Date];
    }
    
    
    if ([model.content containsString:@"@"]) {
        /** 设置@颜色 */
        NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:model.content];
        
        NSMutableArray *modelAtArr = [self checkString:model.content];
        NSLog(@"%@", modelAtArr);
        for (int i = 0; i < modelAtArr.count; i++) {
            NSRange temp = NSRangeFromString(modelAtArr[i]);
            [attribute addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:temp];
        }
        
        [self.labelOfReplyContent setAttributedText:attribute];
        
    }else {
        self.labelOfReplyContent.text = model.content;
        
    }
    
    
    self.imageViewOfUserFace.frame = CGRectMake(20, 20, 50, 50);
    self.imageViewOfUserFace.layer.cornerRadius = self.imageViewOfUserFace.frame.size.width / 2;
    self.imageViewOfUserFace.layer.masksToBounds = YES;
    self.labelOfUserName.frame = CGRectMake(CGRectGetMaxX(self.imageViewOfUserFace.frame) + 20, CGRectGetMinY(self.imageViewOfUserFace.frame), SCREEN_WIDTH - self.imageViewOfUserFace.frame.size.width - 60 - 80, 20);
    self.labelOfReplyTime.frame = CGRectMake(SCREEN_WIDTH - 80 - 20, CGRectGetMinY(self.imageViewOfUserFace.frame), 80, 20);
    
    
    
    
    CGRect contentRect = [model.content boundingRectWithSize:CGSizeMake(CGRectGetMaxX(self.labelOfReplyTime.frame) - CGRectGetMaxX(self.imageViewOfUserFace.frame) - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil];
    replyHeight = contentRect.size.height;
    NSLog(@"%@", NSStringFromCGRect(contentRect));
    self.labelOfReplyContent.frame = CGRectMake(CGRectGetMaxX(self.imageViewOfUserFace.frame) + 20, CGRectGetMaxY(self.labelOfUserName.frame) + 10, CGRectGetMaxX(self.labelOfReplyTime.frame) - CGRectGetMaxX(self.imageViewOfUserFace.frame) - 20, replyHeight);
}

- (NSMutableArray *)checkString:(NSString *)str {
    NSMutableArray *result = [NSMutableArray array];
    NSString *string = str;
    while ([string containsString:@"@"]) {
        //确定@的位置
        NSRange atRange = [string rangeOfString:@"@"];
        NSString *tempStr = [string stringByReplacingCharactersInRange:[string rangeOfString:@"@"] withString:@"#"];
        //确定@之后返回一个新的字符串
        string = [string substringFromIndex:atRange.location + 1];
        if ([string containsString:@" "] || [string containsString:@":"] || [string containsString:@"："]) {
            if ([string containsString:@" "]) {
                NSRange tempRange = [string rangeOfString:@" "];
                NSRange divideRange = {atRange.location, tempRange.location + 1};
                [result addObject:NSStringFromRange(divideRange)];
            }else if ([string containsString:@":"]) {
                NSRange tempRange = [string rangeOfString:@":"];
                NSRange divideRange = {atRange.location, tempRange.location + 1};
                [result addObject:NSStringFromRange(divideRange)];
            }else {
                NSRange tempRange = [string rangeOfString:@"："];
                NSRange divideRange = {atRange.location, tempRange.location + 1};
                [result addObject:NSStringFromRange(divideRange)];
            }
        }else {
            NSRange divideRange = {atRange.location, string.length + 1};
            [result addObject:NSStringFromRange(divideRange)];
            
        }
        string = tempStr;
        
    }
    return result;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    
    
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
